package com.hotelsystem.reservations.persons;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

import com.google.gson.Gson;
import com.hotelsystem.reservations.interfaces.ClientInterface;

public class ClientManagement implements ClientInterface {

	private ArrayList<Client> clients = new ArrayList<Client>();
	private static String clientFolderPath = CLIENTS_FOLDER_PATH;
	private int directoryNumber = 0;

	public void setClientFolderPath(String path) {
		clientFolderPath = path;
	}

	public boolean addClient(Client client) {
		return clients.add(client);
	}

	public boolean removeClient(Client client) {
		return clients.remove(client);
	}

	/**
	 * This method add new created client in File
	 * 
	 * @param client
	 * @author kristian
	 * @return true or false if client is store
	 */
	public boolean addClientInFile(Client client) {

		Gson gson = new Gson();
		String clientJson = gson.toJson(client);
		File clientFile = new File(clientFolderPath, client.getFirstName()
				+ "_" + client.getLastName() + ".json");
		try (FileWriter writer = new FileWriter(clientFile)) {
			writer.write(clientJson);
			return true;
		} catch (Exception e) {
			System.out.println("No file created!");
		}

		return false;
	}

	public void deleteClient(Client client) {
		File directoryFile = new File(clientFolderPath);
		File[] files = directoryFile.listFiles();
		String clientFileName = client.getFirstName() + "_"
				+ client.getLastName() + ".json";
		for (File file : files) {
			if (file.getName().equals(clientFileName)) {
				file.delete();
			}
		}
	}

	/**
	 * This method reads all files in directory that stores clients System
	 * stores clients in json files
	 * 
	 * JSON objects are parsed to Client objects
	 * 
	 * @author krasimir
	 * @return list of clients
	 * 
	 */
	public ArrayList<Client> getClients() {

		File directoryFile = new File(clientFolderPath);
		File[] files = directoryFile.listFiles();

		Gson gson = new Gson();

		for (File file : files) {

			if (file.listFiles() != null) {
				directoryNumber++;
				continue;
			}

			try (Scanner sc = new Scanner(file)) {

				Client cl = gson.fromJson(sc.next(), Client.class);
				clients.add(cl);

			} catch (FileNotFoundException e) {
				System.out.println("Error reading file " + file.getName());
				e.printStackTrace();
			}

		}

		return clients;
	}

	public ArrayList<Client> selectClientsByIndices(int[] numbers) {

		ArrayList<Client> clients = new ArrayList<Client>();

		for (int i = 0; i < numbers.length; i++) {
			clients.add(this.clients.get(numbers[i] - 1));
		}

		return clients;
	}

	public int[] clientIndexSplit(String numbers) {

		String[] newNumbers = numbers.split(", ");
		int[] num = new int[newNumbers.length];

		for (int i = 0; i < newNumbers.length; i++) {
			num[i] = Integer.parseInt(newNumbers[i]);
		}

		return num;
	}

	public int getDirectoryNumber() {
		return directoryNumber; // returns directory found number
	}

	public String printClientsByIndex() {

		StringBuffer toPrint = new StringBuffer();

		for (int i = 0; i < clients.size(); i++) {
			toPrint.append(Integer.toString(i + 1) + " "
					+ clients.get(i).getFirstName() + "\n");
		}

		return toPrint.toString();
	}

}
